#include <thread.h>

static volatile ubase_t tick = 0;

extern void __udelay(uint32_t us);

/* ms转为系统tick */
ubase_t ms_to_tick(uint32_t ms)
{
	float tmp = TICK_SMALL / 1000;

	tmp = tmp * ms;

	return (ubase_t)tmp;
}

void udelay(uint32_t us)
{
	__udelay(us);
}

void mdelay(uint32_t ms)
{
	uint32_t i = 0;

	for (; i < ms; ++i)
		udelay(1000);
}

ubase_t get_tick(void)
{
	return tick;
}

void sleep(uint32_t ms)
{
	thread_sleep(ms);
}

void tick_increase(void)
{
	struct thread *thread;
	register ubase_t level;

	level = interrupt_disable();

	++tick;

	thread = thread_self();

	--thread->remaining_tick;

	if (thread->remaining_tick == 0) {
		interrupt_enable(level);
		yield();
	}
	else
	{
		interrupt_enable(level);
	}

	/* 检查定时器是否超时 */
	timer_timeout_check();
}

